package com.leo.snake;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class Snake {
    BufferedImage right = loadImg("right.png");
    BufferedImage top = loadImg("top.png");
    BufferedImage down = loadImg("down.png");
    BufferedImage left = loadImg("left.png");
    BufferedImage body = loadImg("body.png");

    int MAX_LEN=750;
    int[] x = new int[MAX_LEN];//每节身体的x坐标，下同
    int[] y = new int[MAX_LEN];
    int len;//蛇的长度

    int size = 25;//每个格子的大小
    boolean flag;//转向flag。防止连续转向
    String direction = "R";//方向。R右、L左、T上、D下
    boolean foodFlag;
    int maxX = 900 / 25;//地图宽度
    int maxY = 700 / 25;//地图高度
    boolean isInvincible=false;//是否“无敌”

    public Snake() {
        //初始在屏幕约左上角，长度为3
        len = 3;
        x[0] = 1;
        y[0] = 1;

        x[1] = 2;
        y[1] = 1;

        x[2] = 3;
        y[2] = 1;
    }

    /**
     * 加载图片的工具方法
     *
     * @param name
     * @return
     */
    public BufferedImage loadImg(String name) {
        try {
            return ImageIO.read(Snake.class.getResourceAsStream("/" + name));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 绘画自身
     *
     * @param g
     */
    public void draw(Graphics g) {
        //画身体
        for (int i = 0; i < len - 1; i++) {
            g.drawImage(body, x[i] * size, y[i] * size, null);
        }
        //画头部
        g.drawImage(getHead(), x[(len - 1)] * size, y[(len - 1)] * size, null);
    }

    /**
     * 根据自身前进方向获取对应头部图片
     *
     * @return
     */
    private BufferedImage getHead() {
        if (direction.equals("R"))
            return right;

        if (direction.equals("L"))
            return left;

        if (direction.equals("T"))
            return top;
        if (direction.equals("D"))
            return down;
        return null;
    }

    /**
     * 移动一次。返回1表示移动后吃到了食物。返回2表示失败GAME OVER。返回3表示达到最大长度
     *
     * @param food 食物
     * @return
     */
    public int move(Food food) {
        flag = false;
        if(len>=MAX_LEN){
            return 3;
        }
        int endX = x[0];
        int endY = y[0];
        for (int i = 0; i < len - 1; i++) {
            if (x[i] == x[len - 1] && y[i] == y[len - 1]&&!isInvincible) return 2;
            x[i] = x[i + 1];
            y[i] = y[i + 1];
        }
        if (direction.equals("R")) {
            if (x[len - 1] == maxX - 1) {
                x[len - 1] = 0;
            } else {
                x[len - 1]++;
            }
        } else if (direction.equals("L")) {
            if (x[len - 1] == 0) {
                x[len - 1] = maxX - 1;
            } else {
                x[len - 1]--;
            }
        } else if (direction.equals("T")) {
            if (y[len - 1] == 0) {
                y[len - 1] = maxY - 1;
            } else {
                y[len - 1]--;
            }
        } else if (direction.equals("D")) {
            if (y[len - 1] == maxY - 1) {
                y[len - 1] = 0;
            } else {
                y[len - 1]++;
            }
        }
        if (x[len - 1] == food.x && y[len - 1] == food.y) {
            //将所有位置前进一格,在结尾添加一格
            try{
                for(int j=0;j<food.WIGHT;j++){
                    for (int i = len; i > 0; i--) {
                        x[i] = x[i - 1];
                        y[i] = y[i - 1];
                    }
                    x[0] = endX;
                    y[0] = endY;
                    len++;
                }
            }catch (ArrayIndexOutOfBoundsException e){
                return 3;
            }
            return 1;
        }
        return 0;
    }

    private void inc() {
        foodFlag = true;
    }

    public synchronized void event(char keyChar) {
        if (flag) return;
        if (keyChar == 'D' || keyChar == 'd') {
            if (!direction.equals("L")) {
                direction = "R";
                flag = true;
            }
        } else if (keyChar == 'W' || keyChar == 'w') {
            if (!direction.equals("D")) {
                direction = "T";
                flag = true;
            }
        } else if (keyChar == 'A' || keyChar == 'a') {
            if (!direction.equals("R")) {
                direction = "L";
                flag = true;
            }
        } else if (keyChar == 'S' || keyChar == 's') {
            if (!direction.equals("T")) {
                direction = "D";
                flag = true;
            }
        }
    }
}
